<template>
  <div class="big-box">
    <div class="order-con-box">
      <div class="ticketList-top-btn">
        <!-- <el-button @click="$router.go(-1);" icon="el-icon-arrow-left">返回</el-button> -->
        <el-button class="create" @click="addProtocol('add')" icon="el-icon-plus">创建登陆协议</el-button>
      </div>
      <el-table empty-text="暂无用户协议" v-loading="loadingTable" element-loading-text="拼命加载中" @selection-change="handleSelectionChange" @cell-click="cellClick" ref="multipleTable" :data="$store.state.protocolList.agreeList" border style="width: 100%">
        <el-table-column align="center" type="selection" width="66"></el-table-column>
        <el-table-column align="center" type="index" width="66"></el-table-column>
        <el-table-column align="center" label="名称">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="点击进入详情" placement="top">
              <p style="cursor: pointer;">{{ scope.row.title }}</p>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column align="center" label="发布时间">
          <template slot-scope="scope">{{$moment(scope.row.createTime,'YYYY-MM-DD HH:mm')}}</template>
        </el-table-column>
        <el-table-column align="center" width="120" label="操作">
          <template slot-scope="scope">
            <el-button @click="addProtocol('edit',scope.row)" type="text" style="color:#00B2A9">编辑</el-button>
            <el-button @click="deltedData(scope.row)" type="text" style="color:#f56c6c">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="el-pagination-box" v-if="$store.state.protocolList.amount">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="$pageSizes" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="$store.state.protocolList.amount"></el-pagination>
      </div>
    </div>
    <el-dialog :title="titleName" :visible.sync="addShareAlert">
      <el-form :model="shareForm">
        <el-form-item label-width="100px" label="标题">
          <el-input maxlength="60" v-model.trim="shareForm.title" placeholder="请输入标题"></el-input>
        </el-form-item>
        <el-form-item label-width="100px" label="内容">
          <WangEditor ref="editor" :content="shareForm.xyvote"></WangEditor>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addShareAlert = false">取 消</el-button>
        <el-button @click="creadShare('add')" icon="el-icon-plus" :loading="loadingaddShareAlert" type="primary">创 建</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import GetDataFun from "./../js/GetDataFun.js"; //得到一些数据的方法
import WangEditor from "./../wangEditor/index"; // 引入富文本编辑模块
import { mapGetters } from "vuex";

export default {
  name: "loginProtocol",
  components: { WangEditor },
  computed: {
    ...mapGetters(["disabled", "activityId"])
  },
  data() {
    return {
      shareForm: {
        typeId: 2, // 登陆协议
        activityId:
          this.activityId - 0 || sessionStorage.getItem("activityId") - 0,
        title: "",
        agreementId: "",
        xyvote: ""
      },
      titleName: "",
      addShareAlert: false, //新建分享弹框
      loadingTable: false, //表格
      currentPage: 1, //当前页数
      pageSize: 10, //每页多少条
      ticketType: [], //票种类型
      loadingaddShareAlert: false,
      multipleSelection: []
    };
  },
  created() {
    this.$store.dispatch("setDisabled", false);
    this.handleCurrentChange(this.currentPage);
  },
  methods: {
    async handleCurrentChange(val) {
      this.currentPage = val;
      this.loadingTable = true;
      await GetDataFun.getProtocolList(
        this.currentPage,
        this.pageSize,
        this.shareForm.typeId
      ).then(res => {}, err => {});
      this.loadingTable = false;
    },
    async handleSizeChange(val) {
      this.pageSize = val;
      this.loadingTable = true;
      await GetDataFun.getProtocolList(
        this.currentPage,
        this.pageSize,
        this.shareForm.typeId
      ).then(res => {}, err => {});
      this.loadingTable = false;
    },
    cellClick(row, column) {
      if (column.label == "名称") {
        this.$router.push({
          path: "/proDetails",
          query: {
            data: row.xyvote
          }
        });
      }
    },
    // 打开新建或编辑协议
    addProtocol(e, scrowRow) {
      let subData = {};
      if (e == "add") {
        this.titleName = "创建协议";
        this.$nextTick(_ => {
          this.$refs.editor.clear(); //清空富文本
        });
      } else {
        this.titleName = "编辑协议";
        this.shareForm = Object.assign({}, scrowRow);
        this.$nextTick(_ => {
          this.$refs.editor.setContent();
        });
      }
      this.addShareAlert = true;
    },
    // 提交数据
    creadShare() {
      if (!this.shareForm.title) {
        this.$message({
          message: "请输入标题",
          type: "warning"
        });
        return;
      }
      this.shareForm.xyvote = this.$refs.editor.getContent(); //富文本内容
      if (!this.shareForm.xyvote) {
        this.$message({
          message: "请输入内容",
          type: "warning"
        });
        return;
      }
      this.loadingaddShareAlert = true;
      let subData = this.shareForm;
      subData.isPay = "free";
      subData.activityId = this.$store.state.activityId;
      this.$http.post("/api/isv/agreeMent/createAgreeMent", subData).then(
        res => {
          this.loadingaddShareAlert = false;
          if (res.data.success) {
            this.$message({
              type: "success",
              message: "创建成功"
            });
            this.addShareAlert = false;
            this.shareForm.title = "";
            this.shareForm.xyvote = "";
            this.currentPage = 1;
            this.handleCurrentChange(this.currentPage);
          } else {
            this.$message({
              message: res.data.errorMsg || "创建失败",
              type: "warning"
            });
          }
        },
        err => {
          this.loadingaddShareAlert = false;
          this.$message.error("服务器出小差了");
        }
      );
    },
    //删除
    deltedData(scopeRow) {
      this.$confirm("您确定是否要进行删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let subData = {
            activityId:
              this.activityId - 0 || sessionStorage.getItem("activityId") - 0,
            agreementId: scopeRow.agreementId
          };
          this.$http.post("/api/isv/agreeMent/delAgreeMent", subData).then(
            res => {
              if (res.data.success) {
                this.$message({
                  type: "success",
                  message: "删除成功!"
                });
                this.currentPage = 1;
                this.handleCurrentChange(this.currentPage);
              } else {
                this.$message({
                  message: res.data.errorMsg || "删除失败",
                  type: "warning"
                });
              }
            },
            err => {
              this.$message.error("服务器出小差了");
            }
          );
        })
        .catch(() => {});
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  }
};
</script>

<style scoped>
.big-box {
  width: 100%;
  height: auto;
}
.order-seach-box {
  width: 100%;
  height: auto;
  background: #fff;
  border-radius: 6px;
  padding: 26px;
}
.el-form-box {
  display: flex;
  -webkit-display: flex;
  -webkit-justify-content: flex-start;
  justify-content: flex-start;
  -webkit-align-items: flex-start;
  align-items: flex-start;
  flex-wrap: nowrap;
  width: 100%;
}

.order-con-box {
  width: 100%;
  height: auto;
  min-height: 200px;
  background: #fff;
  border-radius: 6px;
  padding: 26px;
  /* margin-top: 18px; */
}
.ticketList-top-btn {
  width: 100%;
  height: auto;
  margin: 10px 0px 20px 0px;
  display: flex;
  -webkit-display: flex;
  -webkit-justify-content: flex-end;
  justify-content: flex-end;
  -webkit-align-items: center;
  align-items: center;
}
</style>
